//
//  YBErWeiMaDownLoadViewController.m
//  GongXinDangJian
//
//  Created by apple on 17/12/12.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBErWeiMaDownLoadViewController.h"

@interface YBErWeiMaDownLoadViewController ()

@end

@implementation YBErWeiMaDownLoadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titlelabel.text = @"我的二维码";
    self.view.backgroundColor = [UIColor themeGrayColor];
    [self createScanImageWithString:DOWNLOADURL];
    
    UIImageView * headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(ItemWidth * 50, ItemHeight * 40 + navHeight, ItemWidth * 80 , ItemWidth * 80)];
    [headImageView sd_setImageWithURL:CREATEURL(ZJUSERINFORMODEL.ORG_ACCOUNT.PHOTO_URL) placeholderImage:PlaceholderHeaderImage];
    [self.view addSubview:headImageView];
    
    UILabel * nameLabel = [ToolClass creatLabelWithFrame:CGRectMake(ItemWidth * 140, ItemHeight * 40  + navHeight, [UIScreen zj_screenWith] / 2, ItemWidth * 80) font:20 textColor:[UIColor textBlackColor] text:[NSString stringWithFormat:@"%@",ZJUSERINFORMODEL.ORG_ACCOUNT.USER_NAME.length > 0 ? ZJUSERINFORMODEL.ORG_ACCOUNT.USER_NAME : @""]];
    [self.view addSubview:nameLabel];
}

//创建二维码
-(void)createScanImageWithString:(NSString *)string{
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    /**  恢复默认 */
    [filter setDefaults];
    /**  给过滤器添加地址 */
    NSString *dataString = string;
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    /**  设置滤镜数据 */
    [filter setValue:data forKeyPath:@"inputMessage"];
    /**  获取输出的二维码 */
    CIImage *myQrImage = [filter outputImage];
    
    UIImage * image = [self createNonInterpolatedUIImageFormCIImage:myQrImage withSize:[UIScreen zj_screenWith] * 0.45];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,[UIScreen zj_screenWith] * 0.5 ,[UIScreen zj_screenWith] * 0.5)];
    imageView.center = self.view.center;
    imageView.image = image;
    imageView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:imageView];
}

/*
 一些初始化的代码加上实现代理方法便完成了二维码扫描的工作，这里我们需要注意的是， 在二维码扫描的时候， 我们一般都会在屏幕中间放一个方框，用来显示二维码扫描的大小区间，这里我们在个AVCaptureMetadataOutput类中有一个rectOfInterest属性，它的作用就是设置扫描范围。
 
 这个CGRect参数和普通的Rect范围不太一样，它的四个值的范围都是0-1，表示比例。
 rectOfInterest都是按照横屏来计算的 所以当竖屏的情况下 x轴和y轴要交换一下。
 宽度和高度设置的情况也是类似。
 
 */

//将二维码转换成高清图
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}

@end
